﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using EpicBall.ScreenSystem;
using EpicBall.Camera;
using EpicBall.Levels.Classes;
using EpicBall.Levels.Classes.Enemies;

namespace EpicBall.Levels
{
    class Level3 : PhysicsGameScreen
    {
        int screenWidth;
        int screenHeight;

        Texture2D collisionTexture;
        Texture2D blankTexture;
        Texture2D waveTexture;

        Texture2D collectableTexture;
        Texture2D startTexture;
        Texture2D endTexture;

        Texture2D diamondTexture;
        Texture2D rampTexture;

        public Level3() : base(3000, 2500) { _leveltitle = "space"; }

        public override void LoadContent()
        {
            base.LoadContent();
            screenWidth = (int)GameStack.Dimensions.X;
            screenHeight = (int)GameStack.Dimensions.Y;

            collisionTexture = GameStack.Content.Load<Texture2D>("test_level");
            ballTexture = GameStack.Content.Load<Texture2D>("character/ball1/face0");
            blankTexture = GameStack.Content.Load<Texture2D>("blank");

            playerTopArrowTexture = GameStack.Content.Load<Texture2D>("character/player_top_arrow");
            collectableTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/collectable");
            startTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpIn");
            endTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpOut");

            backgroundTexture = GameStack.Content.Load<Texture2D>("backgrounds/space");

            waveTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/World/Sand/wave1");
            diamondTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/diamond");
            rampTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/ramp");

            screenSong = GameStack.Content.Load<Song>("Audio/space");

            _airResistance = new Vector2(2.5f, 0);
            _speedThreshold = 12.5f;

            _levelGold = 45;
            _levelSilver = 30;
            _levelBronze = 15;

            gameWorld.Gravity = Vector2.UnitY * 10;


            MovingPlatform platform = new MovingPlatform(this, GameStack.Content.Load<Texture2D>("Enemies/projectilePlatform"));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(1275, 650)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(1450, 700)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(1450, 900)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(1800, 400)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(2800, 400)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(2800, 2250), 150));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(100, 2250), 100, true, 1500));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(100, 500), 500));
            platform.Ready();
            AddObjectToWorld(platform, new Vector2(1275, 650), 3);

            SpikeTrap spike1 = new SpikeTrap(this);

            //Linear line of spikes
            for (int i = 0; i < 20; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(i*100+50, 50));
            }
            //Down and to right near start
            for (int i = 0; i < 13; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(i * 75 + 50, 300 + i * 75));
            }

            AddObjectToWorld(new LevelObject(this, 300, 20), new Vector2(1175, 1275), 1);

            //Up and to right
            for (int i = 0; i < 7; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(i * 75 + 1450, 970- i*75));
            }

            //Up and to right steeper
            for (int i = 0; i < 4 ; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(1350 + i * 30  , 1300 - i * 85));
            }

            

            //top right box
            AddObjectToWorld(new LevelObject(this, 20, 250), new Vector2(2000, 600), 1);
            AddObjectToWorld(new LevelObject(this, 600, 20), new Vector2(2300, 715), 1);
            AddObjectToWorld(new LevelObject(this, 20, 250), new Vector2(2600, 600), 1);

            AddObjectToWorld(new LevelObject(this, diamondTexture), new Vector2(2590, 715), 1);

            //Down right side
            for (int i = 0; i < 10; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(2675, 525 + i * 100));
            }

            //Into top right box
            for (int i = 0; i < 5; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(2300, i*100));
            }

            //Bottom Right Corner
            for (int i = 0; i < 4; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(i * 100 + 2200, 1900));
            }
            //Bottom Right First up
            for (int i = 0; i < 6; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(2200, 1800 - 100*i));
            }

            //Bottom Right Top
            for (int i = 0; i < 6; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(2150 - i* 100, 1200));
            }

            //Bottom Right First Down
            for (int i = 0; i < 6; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(1600, 1800 - 100 * i));
            }

            //Bottom right platform
            AddObjectToWorld(new LevelObject(this, 1400, 20), new Vector2(2000, 2100), 1);
            //Bottom right platform
            LevelObject stopPlatform = new LevelObject(this, 50, 5);
            stopPlatform.Color = Color.Red;
            AddObjectToWorld(stopPlatform, new Vector2(2675, 2090));

            //Inside bottom right box
            for (int i = 0; i < 8; i++)
            {
                AddObjectToWorld(new SpikeTrap(this), new Vector2(1800, 2240 - 100 * i));
            }

            LevelObject diamond1 = new LevelObject(this, diamondTexture);
            diamond1.Color = Color.ForestGreen;
            AddObjectToWorld(diamond1, new Vector2(1900, 2100), 2);

            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1200, 900), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(2500, 400), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(2500, 1600), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(2500, 1700), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1950, 1900), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1950, 1700), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1400, 1700), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1350, 1800), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(1300, 1600), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 600), 0, 2, 3);//
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 650), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 700), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 750), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 800), 0, 2, 3);
            AddObjectToWorld(new Collectable(this, collectableTexture), new Vector2(100, 1000), 0, 2, 3);


            LevelObject start = new Warp(this, true, startTexture);
            AddObjectToWorld(start, new Vector2(100, 100), 0, 1, 0, true);

            LevelObject end = new Warp(this, false, endTexture);
            AddObjectToWorld(end, new Vector2(100, 500), 0, 1, 10, false);


            //Setup positions

            _startPos = start.Body.Position;
            player.Position = _startPos;

        }


        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (isDead)
            {
                isDead = false;
                LoadContent();
            }
        }
    }
}
